package com.worldcup.model.dto;

import com.worldcup.model.PredictGroup;
import com.worldcup.model.PredictKnockout;
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author duwan.zq
 * @date 2018/06/02
 */
@Getter
@Setter
public class PredictAllDTO {

    @NotBlank
    private String openid;

    private List<PredictTypeGroupDTO> groups;

    private List<PredictTypeKnockoutDTO> knockouts;

    public PredictAllDTO(){
    }

    public PredictAllDTO(String openid, List<PredictGroup> predictGroups, List<PredictKnockout> predictKnockouts) {
        this.openid = openid;
        buildGroups(predictGroups);
        buildKnockouts(predictKnockouts);
    }

    private void buildGroups(List<PredictGroup> predictGroups) {
        if(predictGroups == null){
            return;
        }
        Map<String, List<PredictGroup>> predictGroupMap = predictGroups.stream().collect(Collectors.groupingBy(PredictGroup::getGroupName));
        if(predictGroupMap == null){
            return;
        }
        this.groups = new ArrayList<>(predictGroupMap.size());
        predictGroupMap.forEach((groupName, curPredictGroups) -> this.groups.add(new PredictTypeGroupDTO(groupName, curPredictGroups)));
    }

    private void buildKnockouts(List<PredictKnockout> predictKnockouts) {
        if(predictKnockouts == null){
            return;
        }
        Map<Integer, List<PredictKnockout>> predictKnockMap = predictKnockouts.stream().collect(Collectors.groupingBy(PredictKnockout::getMatchType));
        if(predictKnockMap == null){
            return;
        }
        this.knockouts = new ArrayList<>(predictKnockMap.size());
        predictKnockMap.forEach((matchType, curPredictKnockout) -> this.knockouts.add(new PredictTypeKnockoutDTO(matchType, curPredictKnockout)));
    }

    @Override
    public String toString(){
        return super.toString();
    }

}
